import pygame

from constants.Constants import *
from role.Role import Role


class Missile(Role):

    def __init__(self, tank):
        image_name = "resources/tankmissile.gif"
        if tank.__class__.__name__ != "Player":
            image_name = "resources/enemymissile.gif"

        super().__init__(tank.screen, pygame.image.load(image_name), direction=tank.direction, speed=12)
        self.rect.left = tank.rect.left + (tank.width - self.width) // 2
        self.rect.top = tank.rect.top + (tank.height - self.height) // 2

    def play(self):
        if not self.live:
            return None

        self.draw()

        if self.direction == UP:
            self.rect.top -= self.speed
            if self.rect.top < 0:
                self.live = False

        elif self.direction == DOWN:
            self.rect.top += self.speed
            if self.rect.top > GAME_WINDOW_HEIGHT - self.height:
                self.live = False

        elif self.direction == LEFT:
            self.rect.left -= self.speed
            if self.rect.left < 0:
                self.live = False

        elif self.direction == RIGHT:
            self.rect.left += self.speed
            if self.rect.left > GAME_WINDOW_WIDTH - self.width:
                self.live = False
